#include "Toolbullet.h"

Toolbullet::Toolbullet(int CountBullet, float timetolive, float speed)
{
	m_TypeGun = IDImage::IMG_PlasmaGun;

	for (int i = 0; i < CountBullet; i++)
	{
		Bullet *temp = new Bullet(m_TypeGun, timetolive);
		m_FrameBullet.push_back(temp);
	}


	TimeAnimation = 0.04f;
	m_Speed = speed;
	m_gameTime = new GameTime();
}

void Toolbullet::InitBullet(float x, float y, int width, int height, int GunPositionX, int GunPositionY, SpriteEffect flip, ShootDirection shotdirec, TypeMove type)
{
	m_TypeMove = type;
	m_shootDirect = shotdirec;

	m_Y = (int)y + GunPositionY;

	if (m_shootDirect == ShootDirection::_0_SHOOT || m_shootDirect == ShootDirection::_315_SHOOT
		|| m_shootDirect == ShootDirection::_45_SHOOT || m_shootDirect == ShootDirection::_30_SHOOT
		|| m_shootDirect == ShootDirection::_60_SHOOT || m_shootDirect == ShootDirection::_300_SHOOT
		|| m_shootDirect == ShootDirection::_330_SHOOT)
	{
		if (flip == SpriteEffect::Flip)
		{
			m_X = (int)x - GunPositionX + width;
			m_Auto = Auto::DECREASE;

			if (m_shootDirect == ShootDirection::_0_SHOOT)
			{
				m_shootDirect = ShootDirection::_0_SHOOT;
			}
			else if (m_shootDirect == ShootDirection::_45_SHOOT)
			{
				m_shootDirect = ShootDirection::_315_SHOOT;
			}
			else if (m_shootDirect == ShootDirection::_315_SHOOT)
			{
				m_shootDirect = ShootDirection::_45_SHOOT;
			}
			else if (m_shootDirect == ShootDirection::_330_SHOOT)
			{
				m_shootDirect = ShootDirection::_30_SHOOT;
			}
			else if (m_shootDirect == ShootDirection::_300_SHOOT)
			{
				m_shootDirect = ShootDirection::_60_SHOOT;
			}
			else if (m_shootDirect == ShootDirection::_30_SHOOT)
			{
				m_shootDirect = ShootDirection::_330_SHOOT;
			}
			else if (m_shootDirect = ShootDirection::_60_SHOOT)
			{
				m_shootDirect = ShootDirection::_300_SHOOT;
			}
		}
		else
		{
			m_X = (int)x + GunPositionX;
			m_Auto = Auto::INCREASE;
		}
	}
	else
	{
		if (m_shootDirect == ShootDirection::_90_SHOOT)
			m_Auto = Auto::INCREASE;
		else if (m_shootDirect == ShootDirection::_270_SHOOT)
			m_Auto = Auto::DECREASE;

		if (flip == SpriteEffect::Flip)
			m_X = (int)x - GunPositionX + width;
		else
			m_X = (int)x + GunPositionX;
	}
}


void Toolbullet::UpdateVeclocity()
{
}

void Toolbullet::Update()
{
	m_gameTime->setTime();

	Bullet *temp = NULL;

	if (m_gameTime->getElapsedTimePerSecond() >= TimeAnimation)
	{
		for (unsigned int i = 0; i < m_giveBullet.size(); i++)
		{
			temp = m_giveBullet.front();
			temp->Update();
			m_giveBullet.pop();

			if (!(temp->isAlive()))
				m_FrameBullet.push_back(temp);
			else
				m_giveBullet.push(temp);
		}

		temp = NULL;
		m_gameTime->Update();
	}
}

/*void Toolbullet::Update()
{
	Bullet *temp = NULL;

	if (m_gameTime->getElapsedTimePerSecond() >= TimeAnimation)
	{
		for (unsigned int i = 0; i < m_giveBullet.size(); i++)
		{
			temp = m_giveBullet.front();
			temp->Update();
			m_giveBullet.pop();

			if (!(temp->isAlive()))
				m_FrameBullet.push_back(temp);
			else
				m_giveBullet.push(temp);
		}

		temp = NULL;
		m_gameTime->Update();
	}
}*/

void Toolbullet::Shoot_MachineGun()
{
	if (m_FrameBullet.size() != 0)
	{
		m_giveBullet.push(m_FrameBullet.back());

		m_giveBullet.back()->setPosition(m_X, m_Y, m_shootDirect, m_Auto, 0, m_Speed);
		m_giveBullet.back()->InitTime();

		m_FrameBullet.pop_back();
	}
}

void Toolbullet::setGun(IDGun gun)
{
	switch (gun)
	{
	case IDGun::Plasma_Gun:
		m_TypeGun = IDImage::IMG_PlasmaGun;
		break;
	case IDGun::Spread_Gun:
		m_TypeGun = IDImage::IMG_SpreadGun;
		break;
	case IDGun::Laser_Gun:
		m_TypeGun = IDImage::IMG_Laser;
		break;
	case IDGun::Machine_Gun:
		m_TypeGun = IDImage::IMG_MachineGun;
		break;
	case IDGun::FireBall_Gun:
		m_TypeGun = IDImage::IMG_FireBall;
		break;
	case IDGun::Enemy_Gun:
		m_TypeGun = IDImage::IMG_EnemyGun;
		break;
	}

	for (unsigned int i = 0; i < m_FrameBullet.size(); i++)
	{
		m_FrameBullet[i]->setGun(m_TypeGun);
	}
}

void Toolbullet::Shoot_SpreadGun()
{
	if (m_FrameBullet.size() != 0)
	{
		// lay so vien dan con trong kho 
		int size = m_FrameBullet.size();

		// chi lay <= 5 vien
		if (size > 5) size = 5;

		float angle = 0;
		angle = PI / 6;

		for (int i = 0; i < size; i++)
		{
			m_giveBullet.push(m_FrameBullet.back());

			m_giveBullet.back()->setPosition(m_X, m_Y, m_shootDirect, m_Auto, angle, m_Speed);
			m_giveBullet.back()->InitTime();
			m_FrameBullet.pop_back();

			angle -= PI / 12;
		}
	}
}

void Toolbullet::Shoot()
{
	m_gameTime->Update();

	switch (m_TypeGun)
	{
	case IMG_PlasmaGun:
		m_Speed = SpeedBullet / 2.5f;
		Shoot_MachineGun();
		break;
	case IMG_SpreadGun:
		m_Speed = SpeedBullet / 2.0f;
		Shoot_SpreadGun();
		break;
	case IMG_Laser:
		break;
	case IMG_MachineGun:
		m_Speed = SpeedBullet;
		Shoot_MachineGun();
		break;
	case IMG_FireBall:
		break;
	case IMG_EnemyGun:
		m_Speed = SpeedBullet / 3.0f;
		Shoot_MachineGun();
		break;
	}
}

void Toolbullet::Draw(LPD3DXSPRITE SpriteHandle, int vpx)
{
	Bullet *temp = NULL;
	for (unsigned int i = 0; i < m_giveBullet.size(); i++)
	{
		temp = m_giveBullet.front();
		temp->Draw(SpriteHandle, vpx);
		m_giveBullet.pop();
		m_giveBullet.push(temp);
	}
	temp = NULL;
}

Toolbullet::~Toolbullet()
{
	for (int i = 0; i < m_FrameBullet.size(); i++)
	{
		delete m_FrameBullet[i];
		i--;
	}

	m_FrameBullet.clear();
}
